<?php

m2import('phpinchains/psp/Tag.php');
m2import('phpinchains/tagsupport/URL.php');

class cms_movingwindowpaginatorHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_EMPTY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'cms:movingwindowpaginator';
	var $requiredAttributes = array(
		'name' => '[\w\d_]+',
		'perPage' => '\d+',
		'numResults' => '\d*',
		'length' => '\d+'
	);
	var $attributes = array(
		'name',
		'perPage',
		'numResults',
		'length'
	);
	var $env;
	
	function createRange ($offset, $num) 
	{
		$array = array();
		for ($i = $offset; $i < $offset + $num; $i++)
			$array[] = $i;
		return $array;
	}
	
	function pageRange ($numPages, $totalPages, $currentPage) {
		if ($totalPages <= $numPages) 
			return $this->createRange(1, $totalPages);
		elseif ($currentPage < $numPages / 2)
			return $this->createRange(1, $numPages);
		else if ($currentPage > $totalPages - ($numPages / 2))
			return $this->createRange($totalPages - $numPages + 1, $numPages);
		else
			return $this->createRange($currentPage - floor($numPages / 2), $numPages);
	}
	
	function param($name)
	{
		return $this->actionform->get($name) ? $this->actionform->get($name) : $this->env->param($name);
	}
	
    function doStartTag()
    {
    	$ctx = & phpinchains_ApplicationContext::getInstance();
    	$this->env = & $ctx->getEnv();
    	$name = $this->getAttribute('name');
    	$perPage = (int) $this->getAttribute('perPage');
    	$componentValue = $this->actionform->get($name) ? $this->actionform->get($name) : $this->env->param($name);
		$page  = ((int) $componentValue / $perPage + 1);
		$pages = ceil((int) $this->getAttribute('numResults') / $perPage);
    	if ($pages == 1 || $pages == 0)
    	{
    		return PSP_SKIP_BODY;
    	}
		$range = $this->pageRange(($this->getAttribute('length') ? $this->getAttribute('length') : 5), $pages, $page);
		$paginator = "";
		$url = '?doc_id='.$GLOBALS['doc_id'].'&';
		for ($i = 0; $i < count($range); $i++) {
			if ($page == $range[$i])
				$paginator .= '<span class="current">'.$range[$i].'</span>';
			else
				$paginator .= '<a href="'.$url.'page='.$range[$i].'&'.$name.'='.(($range[$i]-1)*$perPage).'">'.$range[$i].'</a>';
		}
		$str = '<div class="pagination">';		
		if ($page > 2)
			$str .= '<a href="'.$url.'page=1&'.$name.'=0">&lt;&lt;</a>';
		else
			$str .= '<span class="disabled">&lt;&lt;</span>';
		if ($page > 1)
			$str	.= '<a href="'.$url.'page='.($page-1).'&'.$name.'='.(($page-2)*$perPage).'">&lt;</a>';
		else
			$str	.= '<span class="disabled">&lt;</span>';
		$str .= $paginator;
		if ($page < $pages)
			$str .= '<a href="'.$url.'page='.($page+1).'&'.$name.'='.(($page)*$perPage).'">&gt;</a>';
		else
			$str .= '<span class="disabled">&gt;</span>';
		if ($page < $pages - 1)
			$str	.= '<a href="'.$url.'page='.($pages).'&'.$name.'='.(($pages-1)*$perPage).'">&gt;&gt;</a>';
		else
			$str .= '<span class="disabled">&gt;&gt;</span>';
		$str .= '</div>';
		//$str .= "Page: " + page + " of " + pages + "<br/><br/><table border='0'>";
		$this->out->write($str);
    	return PSP_SKIP_BODY;
    }
    
}
?>